<template>
  <a-card :bordered="false">
    <content-header title="门店列表"></content-header>
    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
      :pageSize="15"
    >
      <span slot="action" slot-scope="text, item">
        <template>
          <router-link
            :to="{ path: '/store/applyDetail', query: { id: item.id } }"
          >详情</router-link>
          <a-divider type="vertical" />
          <a @click="handleInto(item)">审核</a>
        </template>
      </span>
    </s-table>
    <a-modal
      title="审核"
      :width="720"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :maskClosable="false"
      @ok="handleSubmit"
      @cancel="handleCancel"
    >
      <a-spin :spinning="confirmLoading">
        <a-form :form="form">
          <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-radio-group
              v-decorator="['status', {rules: [{ required: true }]}]"
            >
              <a-radio :value="0">待审核</a-radio>
              <a-radio :value="1">驳回</a-radio>
              <a-radio :value="2">通过</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-form>
      </a-spin>
    </a-modal>
  </a-card>
</template>

<script>
import * as Api from '@/api/store'
import { ContentHeader, STable } from '@/components'
import CreateForm from './modules/CreateForm'
import moment from 'moment'

export default {
  components: {
    ContentHeader,
    STable,
    CreateForm
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '申请入驻ID',
          dataIndex: 'id'
        },
        {
          title: '门店名称',
          dataIndex: 'store_name'
        },
        {
          title: '地址',
          dataIndex: 'region',
          customRender: (_, item) => {
            return `${item.region} ${item.area}`
          }
        },
        {
          title: '审核状态',
          dataIndex: 'status',
          customRender: (value) => {
            let response = ''
            switch (value) {
              case 0:
                response = '待审核'
                break

              case 1:
                response = '驳回'
                break

              case 2:
                response = '通过'
                break
            }
            return response
          }
        },
        {
          title: '申请时间',
          dataIndex: 'create_at',
          customRender: (value) => {
            return moment(value * 1000).format('LLLL')
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.check(Object.assign(param, this.queryParam))
          .then(response => {
            return response.data.list
          })
      },
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      record: {}
    }
  },
  created () {
  },
  methods: {

    /**
     * 审核门店
     */
    handleInto (item) {
      this.record = item
      this.visible = true
      this.setFieldsValue()
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, form: { setFieldsValue } } = this
      // 设置表单内容
      this.$nextTick(() => {
        // eslint-disable-next-line no-undef
        setFieldsValue(_.pick(record, ['status']))
      })
    },

    /**
     * 刷新列表
     */
    handleRefresh () {
      this.$refs.table.refresh()
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    /**
     * 关闭对话框
     */
    handleCancel () {
      this.visible = false
      // 重置表单内容
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.handleCheck({ form: { id: this.record['id'], status: values.status } })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 2)
          // 关闭对话框事件
          this.handleCancel()
          this.$refs.table.refresh()
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    }
  }
}
</script>
